<?php

namespace app\api\model;


class GoodsModel extends BaseModel
{
    protected $pk = 'id';
    protected $table = 'hs_goods';
    protected static $tableName = 'goods';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    //状态常量
    const STATUS_OPEN = 1;//启用
    const STATUS_CLOSE = 0;//禁用

    //热销常量
    const IS_HOT = 1;//热销
    const NO_HOT = 0;//不热销

    public function getImageAttr($value)
    {
        if ($value) {
            $host = $_SERVER['HTTP_HOST'];
            return 'https://' . $host . $value;
        }
        return '';
    }

    public function getImagesAttr($value)
    {
        if ($value) {
            $host = $_SERVER['HTTP_HOST'];
            $images = explode(',', $value);
            foreach ($images as $k => $v) {
                $images[$k] = 'https://' . $host . $v;
            }
            return $images;
        }
        return '';
    }

    public static function hotList($limit = 0)
    {
        $list = GoodsModel::where('status', self::STATUS_OPEN)->where('is_hot', self::IS_HOT)->field('id,title,category_id,image,price,unit,remark')->order('weigh desc');
        if ($limit) {
            $list = $list->limit($limit);
        }
        $list = $list->select();
        return $list;
    }

    public static function detail($id)
    {
        return self::where('status', self::STATUS_OPEN)->where('id', $id)->field('id,category_id,title,keyword1,keyword2,keyword3,image,images,describe,price,unit,remark')->find();
    }

}